package com.voxel.sense.infomsg.module.firm.details.chars;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.example.com.common.app.PresenterFragment;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.EmptyView;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.view.decoration.SourceCharItemDivider;

import butterknife.BindView;
import cyy.example.com.facotry.constants.IntentConstants;

/**
 * Created by ChenYiYao on 2018/2/27.
 */

public class SourceCharDataFrag extends PresenterFragment<SourceCharDataContract.Presenter>
        implements SourceCharDataContract.View {
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.tv_empty)
    TextView tv_empty;
    @BindView(R.id.empty_view)
    EmptyView empty_view;

    private RecyclerAdapter mAdapter;
    private String mCompanyName;


    @Override
    protected SourceCharDataContract.Presenter initPresenter() {
        return new SourceCharDataPresenter(this);
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.frag_firm_real_data_recycler;
    }

    @Override
    public void onDestroy() {
        mPresenter.destroy();
        super.onDestroy();
    }

    @Override
    protected void initArgs(Bundle bundle) {
        super.initArgs(bundle);
        if (bundle == null)
            return;
        mCompanyName = bundle.getString(IntentConstants.STRING_TYPE);
    }

    @Override
    protected void initData() {
        super.initData();
        //绑定空界面
        empty_view.bind(recycle_view);
        recycle_view.setLayoutManager(new LinearLayoutManager(getContext()));
        recycle_view.addItemDecoration(new SourceCharItemDivider(R.drawable.recycle_divider_10dp_white, R.drawable.recycle_divider));
        mAdapter = new ResourceChartAdapter(new RecyclerAdapter.AdapterListenerImpl() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, Object o) {
                super.onItemClick(holder, o);
            }
        });
        recycle_view.setAdapter(mAdapter);
        //轮询数据
        mPresenter.pollingData(mCompanyName);
    }


    @Override
    public RecyclerAdapter getRecyclerViewAdapter() {
        return mAdapter;
    }

    @Override
    public void onAdapterDataChanged() {
        boolean b = mAdapter.getItemCount() > 0;
        if (b) {
            empty_view.triggerOk();
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }
    }

    @Override
    public void showEmptyView(String desc, boolean btnVisible) {
        boolean b = mAdapter.getItemCount() > 0;
        if (b) {
            empty_view.triggerOk();
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerError(desc);
        }
    }


    @Override
    public void onLoading() {
        empty_view.triggerEmpty();
        empty_view.triggerLoading();
    }

}
